{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>book</th>\n",
       "      <th>title</th>\n",
       "      <th>text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Prologue</td>\n",
       "      <td>\"We should start back,\" Gared urged as the woo...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Bran</td>\n",
       "      <td>The morning had dawned clear and cold, with a ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>Catelyn</td>\n",
       "      <td>Catelyn had never liked this godswood.\\n\\tShe ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>Daenerys</td>\n",
       "      <td>Her brother held the gown up for her inspectio...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>Eddard</td>\n",
       "      <td>The visitors poured through the castle gates i...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>340</th>\n",
       "      <td>5</td>\n",
       "      <td>Jon</td>\n",
       "      <td>Let them die,\" said Queen Selyse.\\n\\tIt was th...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>341</th>\n",
       "      <td>5</td>\n",
       "      <td>Trueborn Lord Of Winterfell.</td>\n",
       "      <td>\"Snow?\" said Tormund Giantsbane. \"You look lik...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>342</th>\n",
       "      <td>5</td>\n",
       "      <td>The Queen'S Hand</td>\n",
       "      <td>The Dornish prince was three days dying.\\n\\tHe...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343</th>\n",
       "      <td>5</td>\n",
       "      <td>Daenerys</td>\n",
       "      <td>The hill was a stony island in a sea of green....</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>344</th>\n",
       "      <td>5</td>\n",
       "      <td>Epilogue</td>\n",
       "      <td>I am no traitor,\" the Knight of Griffin's Roos...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>345 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     book                         title  \\\n",
       "0       1                      Prologue   \n",
       "1       1                          Bran   \n",
       "2       1                       Catelyn   \n",
       "3       1                      Daenerys   \n",
       "4       1                        Eddard   \n",
       "..    ...                           ...   \n",
       "340     5                           Jon   \n",
       "341     5  Trueborn Lord Of Winterfell.   \n",
       "342     5              The Queen'S Hand   \n",
       "343     5                      Daenerys   \n",
       "344     5                      Epilogue   \n",
       "\n",
       "                                                  text  \n",
       "0    \"We should start back,\" Gared urged as the woo...  \n",
       "1    The morning had dawned clear and cold, with a ...  \n",
       "2    Catelyn had never liked this godswood.\\n\\tShe ...  \n",
       "3    Her brother held the gown up for her inspectio...  \n",
       "4    The visitors poured through the castle gates i...  \n",
       "..                                                 ...  \n",
       "340  Let them die,\" said Queen Selyse.\\n\\tIt was th...  \n",
       "341  \"Snow?\" said Tormund Giantsbane. \"You look lik...  \n",
       "342  The Dornish prince was three days dying.\\n\\tHe...  \n",
       "343  The hill was a stony island in a sea of green....  \n",
       "344  I am no traitor,\" the Knight of Griffin's Roos...  \n",
       "\n",
       "[345 rows x 3 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "import pandas as pd\n",
    "\n",
    "books = []\n",
    "chapters_text = []\n",
    "chapters_title = []\n",
    "\n",
    "for i in range(1, 6):\n",
    "    with open(f\"data/GOT{i}.txt\", \"r\") as fp:\n",
    "        book = fp.read()\n",
    "        pattern = r\"\\n{2,}\"\n",
    "        chapters = re.split(pattern, book, flags=re.IGNORECASE)\n",
    "\n",
    "        for e, v in enumerate(chapters):\n",
    "            if e % 2 == 0:\n",
    "                chapters_title.append(v.strip().title())\n",
    "            else:\n",
    "                chapters_text.append(v.strip())\n",
    "                books.append(i)\n",
    "\n",
    "\n",
    "chapters_title = [c for c in chapters_title if c]\n",
    "df = pd.DataFrame.from_dict(\n",
    "    {\"book\": books, \"title\": chapters_title, \"text\": chapters_text}\n",
    ")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Prologue',\n",
       " 'Bran',\n",
       " 'Catelyn',\n",
       " 'Daenerys',\n",
       " 'Eddard',\n",
       " 'Jon',\n",
       " 'Catelyn',\n",
       " 'Arya',\n",
       " 'Bran',\n",
       " 'Tyrion',\n",
       " 'Jon',\n",
       " 'Daenerys',\n",
       " 'Eddard',\n",
       " 'Tyrion',\n",
       " 'Catelyn',\n",
       " 'Sansa',\n",
       " 'Eddard',\n",
       " 'Bran',\n",
       " 'Catelyn',\n",
       " 'Jon',\n",
       " 'Eddard',\n",
       " 'Tyrion',\n",
       " 'Arya',\n",
       " 'Daenerys',\n",
       " 'Bran',\n",
       " 'Eddard',\n",
       " 'Jon',\n",
       " 'Eddard',\n",
       " 'Catelyn',\n",
       " 'Sansa',\n",
       " 'Eddard',\n",
       " 'Tyrion',\n",
       " 'Arya',\n",
       " 'Eddard',\n",
       " 'Catelyn',\n",
       " 'Eddard',\n",
       " 'Daenerys',\n",
       " 'Bran',\n",
       " 'Tyrion',\n",
       " 'Eddard',\n",
       " 'Catelyn',\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'Eddard',\n",
       " 'Sansa',\n",
       " 'Eddard',\n",
       " 'Daenerys',\n",
       " 'Eddard',\n",
       " 'Jon',\n",
       " 'Eddard',\n",
       " 'Arya',\n",
       " 'Sansa',\n",
       " 'Jon',\n",
       " 'Bran',\n",
       " 'Daenerys',\n",
       " 'Catelyn',\n",
       " 'Tyrion',\n",
       " 'Sansa',\n",
       " 'Eddard',\n",
       " 'Catelyn',\n",
       " 'Jon',\n",
       " 'Daenerys',\n",
       " 'Tyrion',\n",
       " 'Catelyn',\n",
       " 'Daenerys',\n",
       " 'Arya',\n",
       " 'Bran',\n",
       " 'Sansa',\n",
       " 'Daenerys',\n",
       " 'Tyrion',\n",
       " 'Jon',\n",
       " 'Catelyn',\n",
       " 'Daenerys',\n",
       " 'Prologue',\n",
       " 'Arya',\n",
       " 'Sansa',\n",
       " 'Tyrion',\n",
       " 'Bran',\n",
       " 'Arya',\n",
       " 'Jon',\n",
       " 'Catelyn',\n",
       " 'Tyrion',\n",
       " 'Arya',\n",
       " 'Davos',\n",
       " 'Theon',\n",
       " 'Daenerys',\n",
       " 'Jon',\n",
       " 'Arya',\n",
       " 'Tyrion',\n",
       " 'Bran',\n",
       " 'Tyrion',\n",
       " 'Sansa',\n",
       " 'Arya',\n",
       " 'Tyrion',\n",
       " 'Bran',\n",
       " 'Catelyn',\n",
       " 'Jon',\n",
       " 'Theon',\n",
       " 'Tyrion',\n",
       " 'Arya',\n",
       " 'Daenerys',\n",
       " 'Bran',\n",
       " 'Tyrion',\n",
       " 'Arya',\n",
       " 'Catelyn',\n",
       " 'Sansa',\n",
       " 'Catelyn',\n",
       " 'Jon',\n",
       " 'Bran',\n",
       " 'Tyrion',\n",
       " 'Theon',\n",
       " 'Arya',\n",
       " 'Catelyn',\n",
       " 'Daenerys',\n",
       " 'Tyrion',\n",
       " 'Davos',\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'Catelyn',\n",
       " 'Bran',\n",
       " 'Arya',\n",
       " 'Daenerys',\n",
       " 'Tyrion',\n",
       " 'Theon',\n",
       " 'Jon',\n",
       " 'Sansa',\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'Catelyn',\n",
       " 'Theon',\n",
       " 'Sansa',\n",
       " 'Davos',\n",
       " 'Tyrion',\n",
       " 'Sansa',\n",
       " 'Tyrion',\n",
       " 'Sansa',\n",
       " 'Daenerys',\n",
       " 'Arya',\n",
       " 'Sansa',\n",
       " 'Theon',\n",
       " 'Tyrion',\n",
       " 'Jon',\n",
       " 'Bran',\n",
       " 'Prologue',\n",
       " 'Jaime',\n",
       " 'Catelyn',\n",
       " 'Arya',\n",
       " 'Tyrion',\n",
       " 'Davos',\n",
       " 'Sansa',\n",
       " 'Jon',\n",
       " 'Daenerys',\n",
       " 'Bran',\n",
       " 'Davos',\n",
       " 'Jaime',\n",
       " 'Tyrion',\n",
       " 'Arya',\n",
       " 'Catelyn',\n",
       " 'Jon',\n",
       " 'Sansa',\n",
       " 'Arya',\n",
       " 'Samwell',\n",
       " 'Tyrion',\n",
       " 'Catelyn',\n",
       " 'Jaime',\n",
       " 'Arya',\n",
       " 'Daenerys',\n",
       " 'Bran',\n",
       " 'Davos',\n",
       " 'Jon',\n",
       " 'Daenerys',\n",
       " 'Sansa',\n",
       " 'Arya',\n",
       " 'Jon',\n",
       " 'Jaime',\n",
       " 'Tyrion',\n",
       " 'Samwell',\n",
       " 'Arya',\n",
       " 'Catelyn',\n",
       " 'Davos',\n",
       " 'Jaime',\n",
       " 'Tyrion',\n",
       " 'Arya',\n",
       " 'Bran',\n",
       " 'Jon',\n",
       " 'Daenerys',\n",
       " 'Arya',\n",
       " 'Jaime',\n",
       " 'Catelyn',\n",
       " 'Samwell',\n",
       " 'Arya',\n",
       " 'Jon',\n",
       " 'Catelyn',\n",
       " 'Arya',\n",
       " 'Catelyn',\n",
       " 'Arya',\n",
       " 'Tyrion',\n",
       " 'Davos',\n",
       " 'Jon',\n",
       " 'Bran',\n",
       " 'Daenerys',\n",
       " 'Tyrion',\n",
       " 'Sansa',\n",
       " 'Tyrion',\n",
       " 'Sansa',\n",
       " 'Jaime',\n",
       " 'Davos',\n",
       " 'Jon',\n",
       " 'Arya',\n",
       " 'Tyrion',\n",
       " 'Jaime',\n",
       " 'Sansa',\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'Daenerys',\n",
       " 'Jaime',\n",
       " 'Jon',\n",
       " 'Arya',\n",
       " 'Samwell',\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'Samwell',\n",
       " 'Jon',\n",
       " 'Sansa',\n",
       " 'Epilogue',\n",
       " 'Prologue',\n",
       " 'The Prophet',\n",
       " 'The Captain Of Guards',\n",
       " 'Cersei',\n",
       " 'Brienne',\n",
       " 'Samwell',\n",
       " 'Arya',\n",
       " 'Cersei',\n",
       " 'Jaime',\n",
       " 'Brienne',\n",
       " 'Sansa',\n",
       " \"The Kraken'S Daughter\",\n",
       " 'Cersei',\n",
       " 'The Soiled Knight',\n",
       " 'Brienne',\n",
       " 'Samwell',\n",
       " 'Jaime',\n",
       " 'Cersei',\n",
       " 'The Iron Captain',\n",
       " 'The Drowned Man',\n",
       " 'Brienne',\n",
       " 'The Queenmaker',\n",
       " 'Arya',\n",
       " 'Alayne',\n",
       " 'Cersei',\n",
       " 'Brienne',\n",
       " 'Samwell',\n",
       " 'Jaime',\n",
       " 'Cersei',\n",
       " 'The Reaver',\n",
       " 'Jaime',\n",
       " 'Brienne',\n",
       " 'Cersei',\n",
       " 'Jaime',\n",
       " 'Cat Of The Canals',\n",
       " 'Samwell',\n",
       " 'Cersei',\n",
       " 'Brienne',\n",
       " 'Jaime',\n",
       " 'Cersei',\n",
       " 'The Princess In The Tower',\n",
       " 'Alayne',\n",
       " 'Brienne',\n",
       " 'Cersei',\n",
       " 'Jaime',\n",
       " 'Samwell',\n",
       " 'Prologue',\n",
       " 'Tyrion',\n",
       " 'Daenerys',\n",
       " 'Jon',\n",
       " 'Bran',\n",
       " 'Tyrion',\n",
       " \"The Merchant'S Man\",\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'Davos',\n",
       " 'Jon',\n",
       " 'Daenerys',\n",
       " 'Reek',\n",
       " 'Bran',\n",
       " 'Tyrion',\n",
       " 'Davos',\n",
       " 'Daenerys',\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'Davos',\n",
       " 'Reek',\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'Daenerys',\n",
       " 'The Lost Lord',\n",
       " 'The Windblown',\n",
       " 'The Wayward Bride',\n",
       " 'Tyrion',\n",
       " 'Jon',\n",
       " 'Davos',\n",
       " 'Daenerys',\n",
       " 'Melisandre',\n",
       " 'Reek',\n",
       " 'Tyrion',\n",
       " 'Bran',\n",
       " 'Jon',\n",
       " 'Daenerys',\n",
       " 'The Prince Of Winterfell',\n",
       " 'The Watcher',\n",
       " 'Jon',\n",
       " 'Tyrion',\n",
       " 'The Turncloak',\n",
       " \"The King'S Prize\",\n",
       " 'Daenerys',\n",
       " 'Jon',\n",
       " 'The Blind Girl',\n",
       " 'A Ghost In Winterfell',\n",
       " 'Tyrion',\n",
       " 'Jaime',\n",
       " 'Jon',\n",
       " 'Daenerys',\n",
       " 'Theon',\n",
       " 'Daenerys',\n",
       " 'Jon',\n",
       " 'Cersei',\n",
       " 'The Queensguard',\n",
       " 'The Iron Suitor',\n",
       " 'Tyrion',\n",
       " 'Jon',\n",
       " 'The Discarded Knight',\n",
       " 'The Spurned Suitor',\n",
       " 'The Griffin Reborn',\n",
       " 'The Sacrifice',\n",
       " 'Victarion',\n",
       " 'The Ugly Little Girl',\n",
       " 'Cersei',\n",
       " 'Tyrion',\n",
       " 'The Kingbreaker',\n",
       " 'The Dragontamer',\n",
       " 'Jon',\n",
       " 'Trueborn Lord Of Winterfell.',\n",
       " \"The Queen'S Hand\",\n",
       " 'Daenerys',\n",
       " 'Epilogue']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chapters_title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>book</th>\n",
       "      <th>title</th>\n",
       "      <th>text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>2</td>\n",
       "      <td>Daenerys</td>\n",
       "      <td>On the walls of Qarth, men beat gongs to heral...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>2</td>\n",
       "      <td>Bran</td>\n",
       "      <td>Meera moved in a wary circle, her net dangling...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>2</td>\n",
       "      <td>Tyrion</td>\n",
       "      <td>The rushes were scratchy under the soles of hi...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>103</th>\n",
       "      <td>2</td>\n",
       "      <td>Arya</td>\n",
       "      <td>Whatever names Harren the Black had meant to g...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>2</td>\n",
       "      <td>Catelyn</td>\n",
       "      <td>The meeting place was a grassy sward dotted wi...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>105</th>\n",
       "      <td>2</td>\n",
       "      <td>Sansa</td>\n",
       "      <td>The longer you keep him waiting, the worse it ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>106</th>\n",
       "      <td>2</td>\n",
       "      <td>Catelyn</td>\n",
       "      <td>It was full dark before they found the village...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>107</th>\n",
       "      <td>2</td>\n",
       "      <td>Jon</td>\n",
       "      <td>The hill jutted above the dense tangle of fore...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>108</th>\n",
       "      <td>2</td>\n",
       "      <td>Bran</td>\n",
       "      <td>Alebelly found him in the forge, working the b...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>109</th>\n",
       "      <td>2</td>\n",
       "      <td>Tyrion</td>\n",
       "      <td>Varys stood over the brazier, warming his soft...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     book     title                                               text\n",
       "100     2  Daenerys  On the walls of Qarth, men beat gongs to heral...\n",
       "101     2      Bran  Meera moved in a wary circle, her net dangling...\n",
       "102     2    Tyrion  The rushes were scratchy under the soles of hi...\n",
       "103     2      Arya  Whatever names Harren the Black had meant to g...\n",
       "104     2   Catelyn  The meeting place was a grassy sward dotted wi...\n",
       "105     2     Sansa  The longer you keep him waiting, the worse it ...\n",
       "106     2   Catelyn  It was full dark before they found the village...\n",
       "107     2       Jon  The hill jutted above the dense tangle of fore...\n",
       "108     2      Bran  Alebelly found him in the forge, working the b...\n",
       "109     2    Tyrion  Varys stood over the brazier, warming his soft..."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[100:110]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
